package com.share.device.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.share.common.core.utils.DateUtils;
import com.share.common.security.utils.SecurityUtils;
import com.share.device.domain.CabinetSlot;
import com.share.device.domain.PowerBank;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.share.device.mapper.CabinetMapper;
import com.share.device.domain.Cabinet;
import com.share.device.service.ICabinetService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 充电宝柜机Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-24
 */
@Service
public class CabinetServiceImpl extends ServiceImpl<CabinetMapper, Cabinet> implements ICabinetService
{
    @Autowired
    private CabinetMapper cabinetMapper;

    /**
     * 查询充电宝柜机
     *
     * @param id 充电宝柜机主键
     * @return 充电宝柜机
     */
    @Override
    public Cabinet selectCabinetById(Long id)
    {
        return cabinetMapper.selectCabinetById(id);
    }

    /**
     * 查询充电宝柜机列表
     *
     * @param cabinet 充电宝柜机
     * @return 充电宝柜机
     */
    @Override
    public List<Cabinet> selectCabinetList(Cabinet cabinet)
    {
        return cabinetMapper.selectCabinetList(cabinet);
    }

    /**
     * 新增充电宝柜机
     *
     * @param cabinet 充电宝柜机
     * @return 结果
     */
    @Override
    public int insertCabinet(Cabinet cabinet)
    {
        cabinet.setCreateTime(DateUtils.getNowDate());
//        String username = SecurityUtils.getUsername();//获取登录人
//        cabinet.setUsername(username);
        return cabinetMapper.insertCabinet(cabinet);
    }

    /**
     * 修改充电宝柜机
     *
     * @param cabinet 充电宝柜机
     * @return 结果
     */
    @Override
    public int updateCabinet(Cabinet cabinet)
    {
        cabinet.setUpdateTime(DateUtils.getNowDate());
        return cabinetMapper.updateCabinet(cabinet);
    }

    /**
     * 批量删除充电宝柜机
     *
     * @param ids 需要删除的充电宝柜机主键
     * @return 结果
     */
    @Override
    public int deleteCabinetByIds(Long[] ids)
    {
        return cabinetMapper.deleteCabinetByIds(ids);
    }

    /**
     * 删除充电宝柜机信息
     *
     * @param id 充电宝柜机主键
     * @return 结果
     */
    @Override
    public int deleteCabinetById(Long id)
    {
        return cabinetMapper.deleteCabinetById(id);
    }

    //获取充电宝下拉框
    @Override
    public List<PowerBank> getPowerBankList() {
        List<PowerBank> powerBankList = cabinetMapper.getPowerBankList();
        return powerBankList;
    }

    //添加插槽
    @Override
    //添加事务注解
    @Transactional
    public String addSlot(CabinetSlot cabinetSlot) {
        if(ObjectUtils.isEmpty(cabinetSlot.getCabinetId())){
            throw new RuntimeException("充电宝柜id不能为空");
        }
        if(ObjectUtils.isEmpty(cabinetSlot.getPowerBankId())){
            throw new RuntimeException("充电宝id不能为空");
        }
        cabinetMapper.addSlot(cabinetSlot);
        //修改充电宝状态
        cabinetMapper.updatePowerBankStatus(cabinetSlot.getPowerBankId());
        return null;
    }

    //查询未删除且启用的插槽
    @Override
    public List<CabinetSlot> getSlotsByCabinetId(Integer cabinetId) {
        List<CabinetSlot> cabinetSlotList = cabinetMapper.getSlotsByCabinetId(cabinetId);
        return cabinetSlotList;
    }


}
